using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.IO;
using LibNDSFormats.NSBMD;
using Tao.OpenGl;
using LibNDSFormats.NSBTX;
using Tao.FreeGlut;
using Tao.Ode;

namespace MKDS_Course_Editor
{
    public partial class nsbmd_came : Form
    {
        int num = 0;
        int num2 = 0;
        string file_2 = "";
        byte[] file_;
        byte[] tex;
        Nsbmd nsbm;
        NsbmdGlRenderer t;
        ListViewItem item;
        ListViewItem[] item2;
        List<double> cameraPositionX = new List<double>();
        List<double> cameraPositionY = new List<double>();
        List<double> cameraPositionZ = new List<double>();
        List<double> lookatPositionX = new List<double>();
        List<double> lookatPositionY = new List<double>();
        List<double> lookatPositionZ = new List<double>();
        public nsbmd_came(string file, ListViewItem i, ListViewItem[] i2)
        {
            file_2 = file;
            file_ = File.ReadAllBytes(file);
            item = i;
            item2 = i2;
            InitializeComponent();
            simpleOpenGlControl1.InitializeContexts();
            simpleOpenGlControl1.MouseWheel += new MouseEventHandler(simpleOpenGlControl1_MouseWheel);
        }
        void simpleOpenGlControl1_MouseWheel(object sender, MouseEventArgs e)
        {
            dist += (float)e.Delta/1000;
            simpleOpenGlControl1.Invalidate();
            RenderFunc1(nsbm, t);
        }
        
        private void nsbmd_Load(object sender, EventArgs e)
        {
            num3 = (item2.Length*2 - 3) / 3;
            num4 = num3;
            lookatPositionX.Add(Convert.ToDouble(Convert.ToDouble(item.SubItems[1].Text)));
            lookatPositionY.Add(Convert.ToDouble(Convert.ToDouble(item.SubItems[2].Text)));
            lookatPositionZ.Add(Convert.ToDouble(Convert.ToDouble(item.SubItems[3].Text)));
            //
            lookatPositionX.Add(Convert.ToDouble(Convert.ToDouble(item.SubItems[7].Text)));
            lookatPositionY.Add(Convert.ToDouble(Convert.ToDouble(item.SubItems[8].Text)));
            lookatPositionZ.Add(Convert.ToDouble(Convert.ToDouble(item.SubItems[9].Text)));
            //
            lookatPositionX.Add(Convert.ToDouble(Convert.ToDouble(item.SubItems[10].Text)));
            lookatPositionY.Add(Convert.ToDouble(Convert.ToDouble(item.SubItems[11].Text)));
            lookatPositionZ.Add(Convert.ToDouble(Convert.ToDouble(item.SubItems[12].Text)));
            for (int i = 0; i < item2.Length; i++)
            {
                cameraPositionX.Add(Convert.ToDouble(item2[i].SubItems[1].Text));
                cameraPositionY.Add(Convert.ToDouble(item2[i].SubItems[2].Text));
                cameraPositionZ.Add(Convert.ToDouble(item2[i].SubItems[3].Text));
            }
            for (int i = 1; i < item2.Length * 2 - 3; i += 2)
            {
                cameraPositionX.Insert(i, (cameraPositionX[i - 1] + cameraPositionX[i + 1]) / 2);
                cameraPositionY.Insert(i, (cameraPositionY[i - 1] + cameraPositionY[i + 1]) / 2);
                cameraPositionZ.Insert(i, (cameraPositionZ[i - 1] + cameraPositionZ[i + 1]) / 2);
            }
           nsbm  = NsbmdLoader.LoadNsbmd(new MemoryStream(file_));
           t = new NsbmdGlRenderer();
           if (file_2 != "")
           {
               if (File.Exists(file_2.Replace(".nsbmd", ".nsbtx")) && nsbm.materials == null)
               {
                   nsbm.materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(new MemoryStream(File.ReadAllBytes(file_2.Replace(".nsbmd", ".nsbtx"))), out nsbm.Textures, out nsbm.Palettes);
               }
           }
           else if (tex != null && nsbm.materials == null)
           {
               nsbm.materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(new MemoryStream(tex), out nsbm.Textures, out nsbm.Palettes);
           }
           try
           {
               nsbm.MatchTextures();
           }
           catch { }
           for (int i = 0; i < nsbm.models[0].Polygons.Count; i++)
           {
               TreeNode q = new TreeNode(nsbm.models[0].Polygons[i].Name);
               q.Checked = true;
               treeView1.Nodes.Add(q);
           }
            RenderFunc1(nsbm, t);
            timer1.Start();
            do
            {
                   //if (num2 == item2.Length-1)
                   //{ num2 = 0; simpleOpenGlControl1.Invalidate(); RenderFunc1(nsbm, t); }
                   //else { num2 += 1; simpleOpenGlControl1.Invalidate(); RenderFunc1(nsbm, t); }
                   // System.Threading.Thread.Sleep(500);
            }
            while (false);
        }
        public static float X = 0.0f;
        public static float Y = 0.0f;
        public static float ang = 0.0f;
        public static float dist = 0.0f;//2.5
        public static float elev = 0.0f;
        private void RenderFunc1(Nsbmd _nsbmd, NsbmdGlRenderer renderer)
        {
            Gl.glViewport(0, 0, this.Width, Height);
            var vp = new[] { 0f, 0f, 0f, 0f };
            Gl.glGetFloatv(Gl.GL_VIEWPORT, vp);
            float aspect = (vp[2] - vp[0]) / (vp[3] - vp[1]);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluPerspective(90.0f, aspect, 0.02f, 32.0f);


            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glEnable(Gl.GL_TEXTURE_2D);

            Gl.glEnable(Gl.GL_ALPHA_TEST);

            Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
            Gl.glEnable(Gl.GL_BLEND);
            Gl.glAlphaFunc(Gl.GL_GREATER, 0f);
            float[] cam = new float[16];
            Gl.glGetFloatv(Gl.GL_PROJECTION_MATRIX, cam);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            if (true)
            {
                Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0);
                Gl.glBegin(Gl.GL_LINES);
                Gl.glColor3f(1.0f, 1.0f, 1.0f);
                Gl.glVertex3f(0.0f, 0.0f, 0.0f);
                Gl.glVertex3f(1.0f, 0.0f, 0.0f);
                Gl.glVertex3f(0.0f, 0.0f, 0.0f);
                Gl.glVertex3f(0.0f, 1.0f, 0.0f);
                Gl.glVertex3f(0.0f, 0.0f, 0.0f);
                Gl.glVertex3f(0.0f, 0.0f, 1.0f);
                Gl.glEnd();
                Gl.glRasterPos3f(1.0f, 0.0f, 0.0f);
                
                //Glut.glutBitmapCharacter(Glut.GLUT_BITMAP_HELVETICA_18, 'X');
                Gl.glRasterPos3f(0.0f, 1.0f, 0.0f);
               // Glut.glutBitmapCharacter(Glut.GLUT_BITMAP_HELVETICA_18, 'Y');
                Gl.glRasterPos3f(0.0f, 0.0f, 1.0f);

                //Glut.glutBitmapCharacter(Glut.GLUT_BITMAP_HELVETICA_18, 'Z');
            }
            //Gl.glTranslatef(X, Y, -dist);
            //Gl.glRotatef(elev, 1, 0, 0);
            //Gl.glRotatef(ang, 0, 1, 0);
            double scale = 1024;//512;
            switch (num)
            {
                case 0:
                    Glu.gluLookAt(cameraPositionX[num2] / scale, cameraPositionY[num2] / scale, cameraPositionZ[num2] / scale, lookatPositionX[0] / scale, lookatPositionY[0] / scale, lookatPositionZ[0] / scale, 0, 1, 0);
                    break;
                case 1:
                    Glu.gluLookAt(cameraPositionX[num2] / scale, cameraPositionY[num2] / scale, cameraPositionZ[num2] / scale, lookatPositionX[1] / scale, lookatPositionY[1] / scale, lookatPositionZ[1] / scale, 0, 1, 0);
                    break;
                case 2:
                    Glu.gluLookAt(cameraPositionX[num2] / scale, cameraPositionY[num2] / scale, cameraPositionZ[num2] / scale, lookatPositionX[2] / scale, lookatPositionZ[2] / scale, lookatPositionZ[2] / scale, 0, 1, 0);
                    break;
            }
            Gl.glClearColor( 0f, 0f, 1f, 1f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            Gl.glDisable(Gl.GL_CULL_FACE);
            Glu.GLUquadric q = Glu.gluNewQuadric();
            Glu.gluSphere(q, 0.0201f, 50, 50);

            try
            {

                var mod = _nsbmd.models[0];
                if (mod == null)
                    return;
                renderer.Model = _nsbmd.models[0];
                renderer.RenderModel(file_2, new NSBTA.NSBTA.NSBTA_File(), new int[0], new int[0], new int[0], new int[0], new int[0], new NSBCA.NSBCA.NSBCA_File(), false, -1, X, Y, dist, elev, ang, new NSBTP.NSBTP.NSBTP_File(), _nsbmd);
                
            }
            catch (Exception ex)
            {
            }
        }
        private void simpleOpenGlControl1_KeyUp(object sender, KeyEventArgs e)
        {
            
            
        }
        

        private void simpleOpenGlControl1_Scroll(object sender, ScrollEventArgs e)
        {
           
        }

        private void simpleOpenGlControl1_Click(object sender, EventArgs e)
        {
            
        }

        private void simpleOpenGlControl1_MouseDown(object sender, MouseEventArgs e)
        {
            
        }

     
        private void simpleOpenGlControl1_KeyDown(object sender, KeyEventArgs e)
        {
            
            
        }

        private void simpleOpenGlControl1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
        }

        private void nsbmd_KeyDown(object sender, KeyEventArgs e)
        {
            
        }
        
        public void MatchTextures2()
        {
            for (var i = 0; i < nsbm.models.Length; i++)
            {
                for (var j = 0; j < nsbm.models[i].Materials.Count; j++)
                {
                    bool gottex = false;
                    bool gotpal = false;
                    foreach (var mat1 in nsbm.materials)
                    {
                        if (j >= nsbm.models[i].Materials.Count)
                            continue;
                        var mat2 = nsbm.models[i].Materials[j];


                        // match texture
                        if (!gottex && mat1.texname.Equals(mat2.texname))
                        {
                            Console.WriteLine("tex '{0}' matched.", mat2.texname);
                            mat1.CopyTo(mat2);

                            gottex = true;
                        }
                        // match palette
                        if (mat2.format != 7 // NB. direct texture has no palette
                            && !gotpal
                            && (mat1.palname).Equals(mat2.palname))
                        {
                            Console.WriteLine("pal '{0}' matched.", mat1.palname);
                            mat2.palname = mat1.palname;
                            mat2.palsize = mat1.palsize;
                            mat2.paldata = mat1.paldata;
                            //mat1->palsize = 0;
                            gotpal = true;
                        }
                        try
                        {
                            for (int q = 0; q < nsbm.materials.ToArray().Length; q++)
                            {

                                if (mat2.palname.Contains(nsbm.materials.ToArray()[q].texname.Replace("nl_", "").Replace("ng_", "")) || nsbm.materials.ToArray()[q].texname.ToLower().Contains(mat2.palname.ToLower().Replace("1_2_pl", "").Replace("ods_", "").Replace("wl_", "").Replace("olg_", "").Replace("frayuka_", "").Replace("nc_", "").Replace("nt_", "").Replace("of6_", "").Replace("oc6_", "").Replace("mo_", "").Replace("nr_", "").Replace("nt_", "").Replace("_t", "").Replace("fra_", "").Replace("mo_", "").Replace("mmc_", "").Replace("pin_", "").Replace("ma_", "").Replace("ndk_", "").Replace("mhyu_", "").Replace("d_", "").Replace("de_", "").Replace("_m_pl", "").Replace("nk_", "").Replace("nd_", "").Replace("nc_", "").Replace("_3_pl", "").Replace("mtw_", "").Replace("_p3_pl", "").Replace("_pl", "").Replace("bt_", "").Replace("fra_", "").Replace("nl_", "").Replace("m_", "").Replace("ods_", "").Replace("obg_", "").Replace("mh_", "").Replace("cho_", "").Replace("ocs_", "").Replace("osf_", "").Replace("vr_", "").Replace("pt_", "").Replace("kusa_", "").Replace("la_", "").Replace("wa_", "").Replace("mbp", "bp").Replace("oc6_", "").Replace("ol6_", "").Replace("agb_", "").Replace("olg_", "").Replace("oyg_", "").Replace("mo_kanban10", "mo_kanban11").Replace("nr_", "").Replace("nt_", "").Replace(".tga", "").Replace("ng_", "").Replace("nc_", "")) || nsbm.materials.ToArray()[q].texname == mat2.palname || (nsbm.materials.ToArray()[q].texname == "ng_mariokart" && mat2.palname == "m_ma_su01_pl") || (nsbm.materials.ToArray()[q].texname == "dash01" && mat2.palname == "dash01_pl"))
                                {
                                    Console.WriteLine("tex '{0}' matched.", nsbm.materials.ToArray()[q].texname);
                                    nsbm.materials.ToArray()[q].CopyTo(mat2);
                                    // mat2.palname = mat1.palname;
                                    // mat2.palsize = mat1.palsize;
                                    // mat2.paldata = mat1.paldata;
                                    //mat2.palname = nsbm.materials.ToArray()[q].palname;
                                    // mat2.palsize = nsbm.materials.ToArray()[q].palsize;
                                    // mat2.paldata = nsbm.materials.ToArray()[q].paldata;

                                    //mat2.texname = nsbm.materials.ToArray()[q].texname;
                                    //mat2.texsize = nsbm.materials.ToArray()[q].texsize;
                                    //mat2.texdata = nsbm.materials.ToArray()[q].texdata;

                                }

                            }
                            /*for (int q = 0; q < nsbm.materials.ToArray().Length; q++)
                            {

                                if (nsbm.models[0].Materials[i].format != 7 // NB. direct texture has no palette
                                    && nsbm.materials.ToList()[q].palname.Contains(mat2.texname))
                                {
                                    Console.WriteLine("pal '{0}' matched.", nsbm.materials.ToArray()[q].palname);
                                    mat2.palname = nsbm.materials.ToArray()[q].palname;
                                    mat2.palsize = nsbm.materials.ToArray()[q].palsize;
                                    mat2.paldata = nsbm.materials.ToArray()[q].paldata;
                                }

                            }*/
                        }
                        catch { }
                        nsbm.models[i].Materials[j] = mat2;
                    }
                }
            }
        }
        public void MatchTextures()
        {
            /*for (var i = 0; i < models.Length; i++)
            {
                for (var j = 0; j < models[i].Materials.Count; j++)
                {
                    bool gottex = false;
                    bool gotpal = false;
                    foreach (var mat1 in materials)
                    {
                        if (j >= models[i].Materials.Count)
                            continue;
                        var mat2 = models[i].Materials[j];


                        // match texture
                        if (!gottex && mat1.texname.Equals(mat2.texname))
                        {
                            Console.WriteLine("tex '{0}' matched.", mat2.texname);
                            mat1.CopyTo(mat2);

                            gottex = true;
                        }
                        // match palette
                        if (mat2.format != 7 // NB. direct texture has no palette
                            && !gotpal
                            && (mat1.palname).Equals(mat2.palname))
                        {
                            Console.WriteLine("pal '{0}' matched.", mat1.palname);
                            mat2.palname = mat1.palname;
                            mat2.palsize = mat1.palsize;
                            mat2.paldata = mat1.paldata;
                            //mat1->palsize = 0;
                            gotpal = true;
                        }
                    }
                }
            }*/
            nsbm.models[0].Materials.Clear();
            nsbm.models[0].Materials.AddRange(nsbm.materials);

            for (int i = 0; i < nsbm.models[0].Materials.Count; i++)
            {
                for (int q = 0; q < nsbm.materials.ToArray().Length; q++)
                {

                    if (nsbm.models[0].Materials[i].format != 7 // NB. direct texture has no palette
                        && nsbm.materials.ToList()[q].palname.Contains(nsbm.models[0].Materials[i].texname))
                    {
                        Console.WriteLine("pal '{0}' matched.", nsbm.materials.ToArray()[q].palname);
                        nsbm.models[0].Materials[i].palname = nsbm.materials.ToArray()[q].palname;
                        nsbm.models[0].Materials[i].palsize = nsbm.materials.ToArray()[q].palsize;
                        nsbm.models[0].Materials[i].paldata = nsbm.materials.ToArray()[q].paldata;                        
                    }
                }
            }
            
            /*for (var i = 0; i < nsbm.models.Length; i++)
            {
                for (var j = 0; j < nsbm.models[i].Materials.Count; j++)
                {
                    bool gottex = false;
                    bool gotpal = false;
                    foreach (var mat1 in nsbm.materials)
                    {
                        if (j >= nsbm.models[i].Materials.Count)
                            continue;
                        var mat2 = nsbm.models[i].Materials[j];

                        // match texture
                        if (!gottex && mat1.texname.Equals(mat2.texname))
                        {
                            Console.WriteLine("tex '{0}' matched.", mat2.texname);
                            mat1.CopyTo(mat2);

                            gottex = true;
                        }
                        for (int q = 0; q < nsbm.materials.ToArray().Length; q++)
                        {

                            if (mat2.format != 7 // NB. direct texture has no palette
                                && !gotpal
                                && nsbm.materials.ToList()[q].palname.Contains(mat2.texname)
                                && (mat1.palname).Equals(mat2.palname))
                            {
                                Console.WriteLine("pal '{0}' matched.", nsbm.materials.ToArray()[q].palname);
                                mat2.palname = nsbm.materials.ToArray()[q].palname;
                                mat2.palsize = nsbm.materials.ToArray()[q].palsize;
                                mat2.paldata = nsbm.materials.ToArray()[q].paldata;
                                //mat1->palsize = 0;
                                gotpal = true;
                            }
                        }
                        nsbm.models[i].Materials[j] = mat2;
                        // match palette
                        //if (mat2.format != 7 // NB. direct texture has no palette
                        //    && !gotpal
                        //    && (mat1.palname).Equals(mat2.palname))
                       // {
                        //    Console.WriteLine("pal '{0}' matched.", mat1.palname);
                        //    mat2.palname = mat1.palname;
                        //    mat2.palsize = mat1.palsize;
                       //     mat2.paldata = mat1.paldata;
                       //     //mat1->palsize = 0;
                       //     gotpal = true;
                        //}
                        // models[i].Materials[j] = mat2;
                    }
                }
            }*/
        }
        public bool wire = false;
        public int fix = 0;
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch(keyData)
            {
                case Keys.Right:
                    ang += 1;
                    simpleOpenGlControl1.Invalidate();
                    RenderFunc1(nsbm, t); ;
                    return true;
                case Keys.Left:
                    ang -= 1;
                    simpleOpenGlControl1.Invalidate();
                    RenderFunc1(nsbm, t); ;
                    return true;
                case Keys.Up:
                    elev += 1;
                    simpleOpenGlControl1.Invalidate();
                    RenderFunc1(nsbm, t); ;
                    return true;
                case Keys.Down:
                    elev -= 1;
                    simpleOpenGlControl1.Invalidate();
                    RenderFunc1(nsbm, t); ;
                    return true;
                case Keys.Z:
                    X -= 0.1f;
                    simpleOpenGlControl1.Invalidate();
                    RenderFunc1(nsbm, t); ;
                    return true;
                case Keys.X:
                    X += 0.1f;
                    simpleOpenGlControl1.Invalidate();
                    RenderFunc1(nsbm, t); ;
                    return true;
                case Keys.A:
                    Y -= 0.1f;
                    simpleOpenGlControl1.Invalidate();
                    RenderFunc1(nsbm, t); ;
                    return true;
                case Keys.S:
                    Y += 0.1f;
                    simpleOpenGlControl1.Invalidate();
                    RenderFunc1(nsbm, t); ;
                    return true;
                case Keys.W:
                    if (wire == false)
                    {
                        Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_LINE);
                        simpleOpenGlControl1.Invalidate();
                        RenderFunc1(nsbm, t); ;
                        wire = true;
                    }
                    else
                    {
                        Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_FILL);
                        simpleOpenGlControl1.Invalidate();
                        RenderFunc1(nsbm, t); ;
                        wire = false;
                    }
                    return true;
                case Keys.Escape:
                    X = 0.0f;
                    Y = 0.0f;
                    ang = 0.0f;
                    dist = 0.0f;
                    elev = 0.0f;
                    simpleOpenGlControl1.Invalidate();
                    RenderFunc1(nsbm, t); ;
                    return true;
                case Keys.F:
                    if (fix == 0)
                    {
                        simpleOpenGlControl1.DestroyContexts();
                        simpleOpenGlControl1.InitializeContexts();
                        simpleOpenGlControl1.Invalidate();
                        nsbm = NsbmdLoader.LoadNsbmd(new MemoryStream(file_));
                        t = new NsbmdGlRenderer();

                        if (file_2 != "")
                        {
                            if (File.Exists(file_2.Replace(".nsbmd", ".nsbtx")) && nsbm.materials == null)
                            {
                                nsbm.materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(new MemoryStream(File.ReadAllBytes(file_2.Replace(".nsbmd", ".nsbtx"))), out nsbm.Textures, out nsbm.Palettes);
                            }
                        }
                        else if (tex != null && nsbm.materials == null)
                        {
                            nsbm.materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(new MemoryStream(tex), out nsbm.Textures, out nsbm.Palettes);
                        }
                        try
                        {
                            MatchTextures();
                        }
                        catch { }
                        
                        RenderFunc1(nsbm, t);
                        fix = 1;
                    }
                    else if(fix == 1)
                    {
                        simpleOpenGlControl1.DestroyContexts();
                        simpleOpenGlControl1.InitializeContexts();
                        simpleOpenGlControl1.Invalidate();
                        nsbm = NsbmdLoader.LoadNsbmd(new MemoryStream(file_));
                        t = new NsbmdGlRenderer();

                        if (file_2 != "")
                        {
                            if (File.Exists(file_2.Replace(".nsbmd", ".nsbtx")) && nsbm.materials == null)
                            {
                                nsbm.materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(new MemoryStream(File.ReadAllBytes(file_2.Replace(".nsbmd", ".nsbtx"))), out nsbm.Textures, out nsbm.Palettes);
                            }
                        }
                        else if (tex != null && nsbm.materials == null)
                        {
                            nsbm.materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(new MemoryStream(tex), out nsbm.Textures, out nsbm.Palettes);
                        }
                        try
                        {
                            MatchTextures2();
                        }
                        catch { }
                        RenderFunc1(nsbm, t);
                        fix = 2;
                    }
                    else if(fix == 2)
                    {
                        simpleOpenGlControl1.DestroyContexts();
                        simpleOpenGlControl1.InitializeContexts();
                        simpleOpenGlControl1.Invalidate();
                        nsbm = NsbmdLoader.LoadNsbmd(new MemoryStream(file_));
                        t = new NsbmdGlRenderer();

                        if (file_2 != "")
                        {
                            if (File.Exists(file_2.Replace(".nsbmd", ".nsbtx")) && nsbm.materials == null)
                            {
                                nsbm.materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(new MemoryStream(File.ReadAllBytes(file_2.Replace(".nsbmd", ".nsbtx"))), out nsbm.Textures, out nsbm.Palettes);
                            }
                        }
                        else if (tex != null && nsbm.materials == null)
                        {
                            nsbm.materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(new MemoryStream(tex), out nsbm.Textures, out nsbm.Palettes);
                        }
                        try
                        {
                            nsbm.MatchTextures();
                        }
                        catch { }
                        RenderFunc1(nsbm, t);
                        fix = 0;
                    }
                    return true;
                default:
                    return base.ProcessCmdKey(ref msg, keyData);
            }
        }

        private void simpleOpenGlControl1_Resize(object sender, EventArgs e)
        {
            simpleOpenGlControl1.Invalidate();
            RenderFunc1(nsbm, t);
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            
        }
       double num3 = 0;
        double num4 = 0;
        private void timer1_Tick_1(object sender, EventArgs e)
        {
            simpleOpenGlControl1.Invalidate();
            RenderFunc1(nsbm, t);
            if (num2 == cameraPositionX.Count-1)
            {
                num2 = 0;
            }
            else
            {
                num2 += 1;
            }
            if (num4 == num2 && num4 < cameraPositionX.Count - 1 && num < 3)
            {
                num4 += num3;
                num += 1;
            }
            else if (num == 2)
            {
                num = 0;
                num4 = num3;
            } 
        }

        private void timer2_Tick(object sender, EventArgs e)
        {

        }

        
    }
}
